package org.dtrd.modules.evaluation.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.DcpDetail;
import org.dtrd.modules.evaluation.entity.bean.DcpListInfo;
import org.dtrd.modules.evaluation.entity.request.DcpListRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdDcpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 密西根糖尿病管理评定量表（DCP） 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-15
 */
@Api(tags = "量表评估")
@RestController
@RequestMapping("/evaluation/dcp/")
public class DcpController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdDcpService dcpService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "查询最新的密西根糖尿病管理评定量表（DCP）详情", response = DcpDetail.class)
    @GetMapping("get")
    public Result<?> getDcpLatest(@RequestParam Integer patientId){
        DcpDetail info = dcpService.getDcpLatest(patientId);
        String logContent = String.format("查询密西根糖尿病管理评定量表（DCP）详情，查询id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询密西根糖尿病管理评定量表（DCP）列表", response = DcpListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询密西根糖尿病管理评定量表（DCP）列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getDcpList(@RequestBody DcpListRequest request){
        List<DcpListInfo> list = dcpService.getDcpList(request);
        String logContent = String.format("查询密西根糖尿病管理评定量表（DCP）列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增密西根糖尿病管理评定量表（DCP）")
    @PostMapping("edit")
    public Result<?> saveOrUpdateDcp(@RequestBody DcpDetail dto){
        DcpDetail info = dcpService.saveOrUpdateDcp(dto);
        String logContent = String.format("新增密西根糖尿病管理评定量表（DCP）, 新增用户id: %d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("删除密西根糖尿病管理评定量表（DCP）")
    @DeleteMapping("remove")
    @AutoLog(value="删除密西根糖尿病管理评定量表（DCP）", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeDcp(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = dcpService.removeDcp(id);
        String logContent = String.format("删除密西根糖尿病管理评定量表（DCP）, 删除记录id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
